package cn.com.thingo.hyintelligent.presenter;

import javax.inject.Inject;

import cn.com.thingo.hyintelligent.base.RxPresenter;
import cn.com.thingo.hyintelligent.base.contract.AddGatewayContract;
import cn.com.thingo.hyintelligent.model.DataManager;
import cn.com.thingo.hyintelligent.model.bean.CommonBean;
import cn.com.thingo.hyintelligent.util.LogUtil;
import cn.com.thingo.hyintelligent.util.RxUtil;
import cn.com.thingo.hyintelligent.util.ToastUtil;
import io.reactivex.functions.Consumer;

public class AddGatewayPresenter extends RxPresenter<AddGatewayContract.View> implements AddGatewayContract.Presenter {


    private DataManager dataManager;


    @Inject
    public AddGatewayPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void addGateway(String token, String number, String name, String username, String password) {
        addSubscribe(dataManager.addGateway(token, number, name, username, password)
                .compose(RxUtil.<CommonBean>rxSchedulerHelper())
                .subscribe(new Consumer<CommonBean>() {
                    @Override
                    public void accept(CommonBean commonBean) throws Exception {
                        if (commonBean.getStatus() == 1) {
                            mView.viewAddGateway(commonBean);
                        } else {
                            ToastUtil.shortShow(commonBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.d(throwable.getMessage());
                    }
                }));
    }
}
